package com.learn.code.juc.forkjoin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : 翌擎科技
 * @date : 2022/8/10 16:05
 */
public class SumExecute {
    public static void main(String[] args) {
        int sumBegin=0,sumEnd=100000000;
        computeByForkJoin(sumBegin,sumEnd);
        computeBySingleThread(sumBegin,sumEnd);
    }

    private static void computeByForkJoin(int sumBegin,int sumEnd){
        ForkJoinPool forkJoinPool=new ForkJoinPool(16);
        long startTime = System.nanoTime();
        Long aLong = SumExecuteCore.doAction(sumBegin, sumEnd, 10000000);
        System.out.println("=======");
        System.out.println("forkJoin任务拆分："+SumExecuteCore.taskCount);
        System.out.println("ForkJoin计算结果："+aLong);
        System.out.println("ForkJoin计算耗时："+(System.nanoTime()-startTime)/1000000);
    }

    private static void computeBySingleThread(int sumBegin, int sumEnd) {
        long computeResult = 0L;
        long startTime = System.nanoTime();
        for (int i = sumBegin; i < sumEnd; i++) {
            computeResult += i;
        }
        System.out.println("======");
        System.out.println("单线程计算结果：" + computeResult);
        System.out.println("单线程计算耗时：" + (System.nanoTime() - startTime) / 1000000);

    }
}
